﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Gds.Utility.Helper
{
    public static class XmlHelper
    {
        public static string Path = Directory.GetCurrentDirectory() + "/XmlFile.txt";
        public static void SerializeMethod<T>(List<T> listObject)
        {
            try
            {
                FileStream fs = new FileStream(Path, FileMode.Create);
                XmlSerializer xs = new XmlSerializer(typeof(List<T>));
                xs.Serialize(fs, listObject);
                fs.Close();
            }
            catch (Exception ex)
            {
                PublicHelper.WriteErrorMsg("保存文件时异常：" + ex.Message + "，信息：" + ex.StackTrace);
            }
        }
        public static void SerializeMethod<T>(List<T> listObject, string savePath)
        {
            try
            {
                FileStream fs = new FileStream(savePath, FileMode.Create);
                XmlSerializer xs = new XmlSerializer(typeof(List<T>));
                xs.Serialize(fs, listObject);
                fs.Close();
            }
            catch (Exception ex)
            {
                PublicHelper.WriteErrorMsg("保存文件时异常：" + ex.Message + "，信息：" + ex.StackTrace);
            }
        }
        public static void SerializeMethod<T>(ObservableCollection<T> colObject, string path)
        {
            try
            {
                FileStream fs = new FileStream(path, FileMode.Create);
                XmlSerializer xs = new XmlSerializer(typeof(ObservableCollection<T>));
                xs.Serialize(fs, colObject);
                fs.Close();
            }
            catch (Exception ex)
            {
                PublicHelper.WriteErrorMsg("保存文件时异常：" + ex.Message + "，信息：" + ex.StackTrace);
            }
        }
        public static void SerialzeModel<T>(T Model, string Path)
        {
            try
            {
                File.Delete(Path);
                using (FileStream fs = new FileStream(Path, FileMode.Create))
                {
                    XmlSerializer xs = new XmlSerializer(typeof(T));
                    xs.Serialize(fs, Model);
                    fs.Close();
                }
            }
            catch (Exception ex)
            {
                PublicHelper.WriteErrorMsg("保存文件时异常：" + ex.Message + "，信息：" + ex.StackTrace);
            }
        }
        public static ObservableCollection<T> DeserializeMethod<T>(string path)
        {
            ObservableCollection<T> listPers = new ObservableCollection<T>();
            try
            {
                if (File.Exists(path) == true)
                {
                    FileStream fs = new FileStream(path, FileMode.Open);
                    XmlSerializer xs = new XmlSerializer(typeof(ObservableCollection<T>));
                    listPers = xs.Deserialize(fs) as ObservableCollection<T>;
                    fs.Close();
                }
            }
            catch (Exception ex)
            {
                PublicHelper.WriteErrorMsg("读取文件时异常：" + ex.Message + "，信息：" + ex.StackTrace);
            }
            return listPers;
        }
        public static List<T> DeserializeList<T>(string path)
        {
            List<T> listPers = new List<T>();
            try
            {
                if (File.Exists(path) == true)
                {
                    FileStream fs = new FileStream(path, FileMode.Open);
                    XmlSerializer xs = new XmlSerializer(typeof(List<T>));
                    listPers = xs.Deserialize(fs) as List<T>;
                    fs.Close();
                }
            }
            catch (Exception ex)
            {
                PublicHelper.WriteErrorMsg("读取文件时异常：" + ex.Message + "，信息：" + ex.StackTrace);
            }
            return listPers;
        }
        public static T DeserializeModel<T>(string path)
        {

            T tmpModel = default(T);
            try
            {
                if (File.Exists(path) == true)
                {
                    FileStream fs = new FileStream(path, FileMode.Open);
                    XmlSerializer xs = new XmlSerializer(typeof(T));
                    tmpModel = (T)xs.Deserialize(fs);
                    fs.Close();
                }
            }
            catch (Exception ex)
            {
                PublicHelper.WriteErrorMsg("读取文件时异常：" + ex.Message + "，信息：" + ex.StackTrace);
            }
            return tmpModel;
        }
    }
}
